package cmd

import (
	"xcloud-oauth2-server/models"
	"xcloud-oauth2-server/util/migrations"

	"go.uber.org/zap"
)

func Migrate(configFile string) error {

	logger, _ := zap.NewProduction()
	defer logger.Sync() // flushes buffer, if any
	sugar := logger.Sugar()

	_, db, err := initConfigDB(configFile)
	if err != nil {
		return err
	}

	sugar.Info(" ---------- Migrate bootstrap start ----------")
	if err := migrations.Bootstrap(db); err != nil {
		return err
	}
	sugar.Info(" ---------- Migrate bootstrap end ----------")

	sugar.Info(" ---------- Migrate all start ----------")
	// Run migrations for the oauth service
	if err := models.MigrateAll(db); err != nil {
		return err
	}
	sugar.Info(" ---------- Migrate all end  ----------")
	return nil
}
